<script>
	function submit_topic_form() {
		if ($('#topic_form input[name=preview]').val() == 1) {
			return true;
		} else {
			$('#topic_form_ajax').css('visibility', 'visible');
			$.post(
				'<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'ajax_add_topic'))); ?>',
				$('#topic_form').serialize(),
				function(data) {
					if (data.status == 'error') {
						$('#topic_form_msg').html('<div class="msg msg-error">'+data.message+'</div>');
						$('#topic_form_ajax').css('visibility', 'hidden');
					} else {
						var redirect = '<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'goto', 'id' => '{message_id}'))); ?>';
						redirect = redirect.replace('{message_id}', data.message_id);
						setTimeout(function() {
							window.location.href = mcmf_route_redirect.replace('{url}', base64_encode(redirect));
						}, mcmf_timeout_default);
						$('#topic_form_msg').html('<div class="msg msg-success">Тема создана. Перенаправление...</div>');
						$('#topic_form_ajax').css('visibility', 'hidden');
					}
				}, 'JSON'
			);
			return false;
		}
	}
</script>

<div class="padding">
	<p class="pagination crumbs clearfix">
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(), TRUE); ?>" title="Форум Nissan Cedric">Форум</a>
		&raquo;
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'section', 'id' => $cat->section_id)), TRUE); ?>" title="<?php echo HTML::chars($cat->section->name); ?>"><?php echo $cat->section->name; ?></a>
		&raquo;
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'cat', 'id' => $cat->id, 'page' => Cookie::get_page('frontend_forum_cat_'.$cat->id.'_page'))), TRUE); ?>" title="<?php echo HTML::chars($cat->name); ?>"><?php echo Text::limit_chars($cat->name, 32); ?></a>
		&raquo;
	</p>
	<h1>Предпросмотр темы</h1>
</div>

<div id="message_preview" class="padding">
	<table class="forum forum_topic">
		<thead>
			<tr>
				<td class="fsmini">Автор</td>
				<td class="fsmini">Сообщение</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td class="author fsmini">
					<div class="userpic">
						<?php echo Model_User::html_avatar(Model_Reg::get('user')); ?>
					</div>
					<div class="userlink">
						<a href="<?php echo URL::site(Route::get('frontend_user')->uri(array(
							'action' => 'view',
							'id' => Model_Reg::get('user')->id,
						))); ?>"><?php echo Model_User::nickname(Model_Reg::get('user')); ?></a>
					</div>
					<div class="userdata fclight">
						<span class="str">Сообщения: <?php echo intval(Model_Reg::get('user')->forum_total_messages); ?></span>
						<?php if ( ! empty(Model_Reg::get('user')->country)) echo '<span class="str">Страна: '.Model_Reg::get('user')->country.'</span>'; ?>
						<?php if ( ! empty(Model_Reg::get('user')->city)) echo '<span class="str">Город: '.Model_Reg::get('user')->city.'</span>'; ?>
					</div>
				</td>
				<td class="message">
					<div class="date fclight fsmini">
						<?php echo Date::format_timezone(time()); ?>
					</div>
					<div class="msgtext"><?php echo BBCode::toXHTML(isset($_POST['message']['text']) ? trim(strip_tags($_POST['message']['text'])) : $message->text); ?></div>
				</td>
			</tr>
		</tbody>
	</table>
</div>

<div id="topic_form_start" class="padding">
	<div id="topic_form_msg"><?php if (empty($_POST['topic']['name'])) echo '<div class="msg msg-error">Введите название темы.</div>'; elseif (empty($_POST['message']['text'])) echo '<div class="msg msg-error">Введите текст.</div>'; ?></div>
	<form id="topic_form" class="default" action="<?php echo Request::current()->url(); ?>#message_preview" method="post" onsubmit="return submit_topic_form();">
		<fieldset>
			<legend>Добавить сообщение</legend>
			<input type="hidden" name="preview" value="0" />
			<input type="hidden" name="topic[section_id]" value="<?php echo $cat->section_id; ?>" />
			<input type="hidden" name="topic[cat_id]" value="<?php echo $cat->id; ?>" />
			<table class="cols">
				<tr>
					<td class="col col-topic-form-fields">
						<div class="row">
							<div class="label">Название темы:</div>
							<div class="field"><input type="text" name="topic[name]" value="<?php echo isset($_POST['topic']['name']) ? trim(strip_tags($_POST['topic']['name'])) : ''; ?>" /></div>
						</div>
						<div class="row">
							<div class="field"><textarea id="markItUp" name="message[text]"><?php echo isset($_POST['message']['text']) ? trim(strip_tags($_POST['message']['text'])) : ''; ?></textarea></div>
							<script>$('#markItUp').markItUp(markitupSettings);</script>
						</div>
						<div class="row row-button">
							<input type="submit" name="save" value="Предпросмотр" onclick="$('#topic_form input[name=preview]').val('1');" />
							<input type="submit" name="save" value="Опубликовать" onclick="$('#topic_form input[name=preview]').val('0');" />
							<img id="topic_form_ajax" class="ajax_icon" src="/media/img/ajax.gif" alt="" />
							<label class="box fssmall"><input type="checkbox" name="message[subscribe]" value="1" <?php if ((isset($_POST['message']['subscribe']) AND $_POST['message']['subscribe'] == 1) OR ( ! isset($_POST['message']))) echo 'checked="checked"'; ?> /> Получать комментарии на e-mail</label>
						</div>
					</td>
				</tr>
			</table>
		</fieldset>
	</form>
</div>